package com.example.my_helper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    final static int ON_CONNECT = 0;
    final static int ON_MESSAGE = 1;
    final static int ON_SEND = 2;
    final static int ON_CLOSE = 3;
    final static int ON_ERROR = 4;
    final static int NOT_FOUND = 5;
    final static int NO_PERMISSION = 6;
    final static int CONFIG_ERROR = 7;
    final static int UNSUPPORT = 8;

    //ui
    public static String[] title = {"串口调试", "网络调试", "modbus调试", "MQTT客户端", "蓝牙客户端"};
    public View serial_view, network_view, modbus_view, mqtt_view, bluetooth_view;
    public ViewPager pager;
    public List<View> viewList;//view数组
    public TabLayout tabLayout;

    static boolean inited = false;

    SerialDebuger serialDebuger;
    NetworkDebuger networkDebuger;
    ModbusDebuger modbusDebuger;
    MQTTDebuger mqttDebuger;
    BluetoothDebuger bluetoothDebuger;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setAndroidNativeLightStatusBar(this, true);
        UI_Init();
        MyApp.myDBHelper = new DBHelper(getApplicationContext());
        MyApp.myDBHelper.open();
        serialDebuger = new SerialDebuger(this);
        networkDebuger = new NetworkDebuger(this);
        modbusDebuger = new ModbusDebuger(this);
        mqttDebuger = new MQTTDebuger(this);
        bluetoothDebuger = new BluetoothDebuger(this);
        MyApp.modbusDebuger = modbusDebuger;
        MyApp.myUARTDriver = new MyUARTDriver(getApplicationContext(), serialDebuger.handler);
        MyApp.myTCPClient = new MyTCPClient(getApplicationContext(), networkDebuger.handler);

    }


    public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    public void UI_Init()
    {
        pager = (ViewPager) findViewById(R.id.viewpager1);
        LayoutInflater inflater = getLayoutInflater();
        serial_view = inflater.inflate(R.layout.serial_debuger, null);
        network_view = inflater.inflate(R.layout.network_debuger, null);
        modbus_view = inflater.inflate(R.layout.modbus_debuger, null);
        mqtt_view = inflater.inflate(R.layout.mqtt_debuger, null);
        bluetooth_view = inflater.inflate(R.layout.bluetooth_debuger, null);
        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(serial_view);
        viewList.add(network_view);
        viewList.add(modbus_view);
        viewList.add(mqtt_view);
        viewList.add(bluetooth_view);
        tabLayout = (TabLayout) findViewById(R.id.tapLayout1);
        tabLayout.addTab(tabLayout.newTab().setText(title[0]));
        tabLayout.addTab(tabLayout.newTab().setText(title[1]));
        tabLayout.addTab(tabLayout.newTab().setText(title[2]));
        tabLayout.addTab(tabLayout.newTab().setText(title[3]));
        tabLayout.addTab(tabLayout.newTab().setText(title[4]));

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return viewList.size();
            }
            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                // TODO Auto-generated method stub
                container.removeView(viewList.get(position));
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(viewList.get(position));
                return viewList.get(position);
            }
            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        };
        pager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(pager);//与ViewPage建立关系
        tabLayout.getTabAt(0).select();
    }
}